package com.davidhigenbottam.HeatMap.client;

import com.davidhigenbottam.HeatMap.shared.Query;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class QueryUI {

	public static final String DEFAULT_METADATA = "Please choose a data source for the heat map.";

	UI ui = UI.getInstance();

	public QueryUI() {

		
		DataSources.getInstance().addClickHandlers(this);
		
		ui.addYears();
		// Add a handler to send the name to the server
		ui.getCreateHeatMapButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				runHeatmapQuery();
			}
		});
		
		
		
		
		//default
		setupHigherEducationEnrollmentQueryUI();
	}
	
//	private void showAllOverlayTypes(){
//		
//		ui.clearTypeOfOverlay();
//		ui.showTypeOfOverlay();
//		ui.addTypeOfOverlay(Query.OVERLAY_PROVINCE_HEATMAP_DESC, Query.OVERLAY_PROVINCE_HEATMAP);
//		ui.addTypeOfOverlay(Query.OVERLAY_INSTITUTION_SYMBOL_DESC, Query.OVERLAY_INSTITUTION_SYMBOL);
//		
//	}
	private void setupStaffWithPhdQueryUI(){
		ui.hideYear();
		ui.hideLevelOfEducation();
		ui.hideInstituteType();
		ui.hideInstituteOwnership();
		
		//showAllOverlayTypes();

		
		
	}
	private void setupHigherEducationEnrollmentQueryUI(){
		ui.showYear();
		ui.showLevelOfEducation();
		ui.showInstituteType();
		ui.showInstituteOwnership();
		//ui.showTypeOfOverlay();

		//showAllOverlayTypes();
		
		ui.clearLevelOfEducation();
	
		for (String key : Query.studyLevels.keySet())
			ui.addLevelOfEducation(Query.studyLevels.get(key), key);

		ui.clearInstituteType();
		
		for (String key : Query.instituteTypes.keySet())
			ui.addInstituteType(Query.instituteTypes.get(key), key);
		
		ui.clearInstituteOwnership();
		
		for (String key : Query.instituteOwnerships.keySet())
			ui.addInstituteOwnership(Query.instituteOwnerships.get(key), key);
		
	}

	private void setupRatioCommon(){
		ui.showYear();
		ui.showLevelOfEducation();
		
		ui.clearInstituteType();
		ui.addInstituteType("all", 0+"");
		ui.hideInstituteType();
		
		ui.clearInstituteOwnership();
		ui.addInstituteOwnership("all", 0+"");
		ui.hideInstituteOwnership();
		
		ui.clearLevelOfEducation();
		
//		ui.clearTypeOfOverlay();
//		ui.showTypeOfOverlay();
//		ui.addTypeOfOverlay(Query.OVERLAY_PROVINCE_HEATMAP_DESC, Query.OVERLAY_PROVINCE_HEATMAP);
	}
	private void setupEnrollmentRatioQueryUI(){
		setupRatioCommon();
		for(String key: Query.levelsOfEducation.keySet()){
			ui.addLevelOfEducation(Query.levelsOfEducation.get(key), key);
		}
	}
	private void setupStudentEnrollmentRatioQueryUI(){
		setupRatioCommon();
		for(String key: Query.levelsOfEducationSER.keySet()){
			ui.addLevelOfEducation(Query.levelsOfEducationSER.get(key), key);
		}
	}	
	
	public void dataSourceListChanged() {
		
		String ds= DataSources.getInstance().getDataSourceStr();
		

		if (ds.equals(Query.HIGHER_EDUCATION_ENROLLMENT)) {
			setupHigherEducationEnrollmentQueryUI();
		}
		else if(ds.equals(Query.STAFF_WITH_PHD_DATASOURCE)){
			setupStaffWithPhdQueryUI();
		}
		else if (ds.equals(Query.GROSS_ENROLLMENT_RATIO_DATASOURCE)
				|| ds.equals(Query.NET_ENROLLMENT_RATIO_DATASOURCE)) { 	
			setupEnrollmentRatioQueryUI();															
		} else {// SER
			setupStudentEnrollmentRatioQueryUI();
		}

		ui.showQueryControls();
		
		
	}

	private void runHeatmapQuery() {
		ui.showWaiting();
		HeatMap.overlays.runQuery(createQuery());
	}
	
	private Query createQuery(){
		return Query.createQuery(ui.getYear(),
				ui.getLevelOfEducation(),ui.getInstitutionType(), ui.getInstitutionOwnership());
	}
	


}
